<%-- 
    Document   : account
    Created on : May 2, 2013, 10:00:25 PM
    Author     : Lord Makuta
--%>

<%@page import="java.util.ArrayList"%>
<%@page import="db.UserDAO"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="db.DB"%>
<%@page import="db.MovieObj"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<%
    if (session.getAttribute("user")==null)
    {
        response.sendRedirect("register.jsp");
        return;
    }
    else
    {
        UserDAO tmp=UserDAO.loginUser(((UserDAO)session.getAttribute("user")).getEmail(), ((UserDAO)session.getAttribute("user")).getPassword());
        session.setAttribute("user", tmp);
        application.setAttribute("usser", session.getAttribute("user"));
    }
%>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<meta http-equiv="content-language" content="en" />
	<meta name="description" content="" />
	<meta name="language" content="en" />
	
        <title>DataFlix - My Account</title>
	<link rel="shortcut icon" href="http://www.iconarchive.com/download/i59247/franksouza183/fs/Apps-firefox.ico"/>
	
	<link type="text/css" rel="stylesheet" href="./styles/fonts.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/header.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/fancybox.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/images.css"/>

        <link type="text/css" rel="stylesheet" href="./styles/account.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/charcoal.css"/>
	
    </head>
    <body>
        
        <jsp:include page="./header.jsp" />
        
        <div class="centerwrap">
            <form id="info" method="post" action="http://localhost:8080/dataflix/MyAccount">
                <table><tbody>
                    <caption>Edit your personal information</caption>
                    <tr>
                        <td>&nbsp;First name</td>
                        <td><input name="fname" type="text" value="${user.getFirstname()}"/></td>
                        <td>&nbsp;Home address</td>
                        <td><input name="address" type="text" value="${user.getAddress()}"/></td>
                        <td>&nbsp;Telephone number</td>
                        <td><input name="phone" type="text" value="${user.getPhone()}"/></td>
                    </tr>
                    <tr>
                        <td>&nbsp;Last name</td>
                        <td><input name="lname" type="text" value="${user.getLastname()}"/></td>
                        <td>&nbsp;City</td>
                        <td><input name="city" type="text" value="${user.getCity()}"/></td>
                        <td><span class="actionClr">*</span>Credit card number</td>
                        <td><input name="credit" type="password" value="${user.getCreditCardNumber()}"/></td>
                    </tr>
                    <tr>
                        <td><span class="actionClr">*</span>EMail Address</td>
                        <td><input name="email" type="text" value="${user.getEmail()}"/></td>
                        <td>&nbsp;Zip code</td>
                        <td><input name="zip" type="text" value="${user.getZip()}"/></td>
                        <td>&nbsp;Rental plan</td>
                        <td><select name="plan">
                            <option value="0" ${user.getLevel()==0?"selected='selected'":""}>Limited     - $10/month</option>
                            <option value="1" ${user.getLevel()==1?"selected='selected'":""}>Unlimited 1 - $15/month</option>
                            <option value="2" ${user.getLevel()==2?"selected='selected'":""}>Unlimited 2 - $20/month</option>
                            <option value="3" ${user.getLevel()==3?"selected='selected'":""}>Unlimited 3 - $25/month</option>
                        </select></td>
                    </tr>
                    <tr>
                        <td><span class="actionClr">*</span>Password</td>
                        <td><input name="pass" type="password" value="${user.getPassword()}"/></td>
                        <td>&nbsp;State</td>
                        <td><select name="state">
                            <option value=""   ${user.getState()==null?"selected='selected'":""}>Your State</option>
                            <option value="NY" ${user.getState()=="NY"?"selected='selected'":""}>NY - New York</option>
                            <option value="NJ" ${user.getState()=="NJ"?"selected='selected'":""}>NJ - New Jersey</option>
                            <option value="CT" ${user.getState()=="CT"?"selected='selected'":""}>CT - Connecticut</option>
                            <option value="FL" ${user.getState()=="FL"?"selected='selected'":""}>FL - Florida</option>
                            <option value="CA" ${user.getState()=="CA"?"selected='selected'":""}>CA - California</option>
                        </select></td>
                        <td>&nbsp;Rating=${user.getRating()}</td>
                        <td><span class="actionClr">*</span> indicates required field</td>
                    </tr>
                    
                </tbody></table>
                <input id="infoSub" name="save" value="Save changes" type="submit"/>
            </form>
            
            <%
                int uId=(int)((UserDAO)session.getAttribute("user")).getId();
                ArrayList<MovieObj> alQ=new ArrayList<MovieObj>();
                ArrayList<MovieObj> alP=new ArrayList<MovieObj>();
                ArrayList<MovieObj> alR=new ArrayList<MovieObj>();
                
                DB data=new DB();
                Connection conn=null;
                
                try
                {
                    MovieObj tmp=new MovieObj();
                    conn=data.getConnection();
                    Statement stm=conn.createStatement();
                    
                    ResultSet res=stm.executeQuery("SELECT q.MovieId, m.Name, m.Type FROM Movie m, MovieQ q WHERE q.MovieId=m.Id AND q.AccountId="+uId);
                    while (res.next())
                    {
                        tmp=new MovieObj(res.getInt(1), res.getString(2), res.getString(3));
                        alQ.add(tmp);
                    }
                    
                    res=stm.executeQuery("SELECT r.MovieId, m.Name, m.Type FROM Movie m, Rental r WHERE r.ReturnDate IS NULL AND m.Id=r.MovieId AND r.CustomerId="+uId);
                    while (res.next())
                    {
                        tmp=new MovieObj(res.getInt(1), res.getString(2), res.getString(3));
                        alR.add(tmp);
                    }
                    
                    res=stm.executeQuery("SELECT r.MovieId, m.Name, m.Type FROM Movie m, Rental r WHERE r.ReturnDate IS NOT NULL AND m.Id=r.MovieId AND r.CustomerId="+uId);
                    while (res.next())
                    {
                        tmp=new MovieObj(res.getInt(1), res.getString(2), res.getString(3));
                        alP.add(tmp);
                    }
                }
                catch (Exception e)
                {
                    
                }
                finally
                {
                    conn.close();
                    conn=null;
                }
            %>        
            
            <table id="movieBlock"><tbody>
                <td><form id="curRent" method="post" action="http://localhost:8080/dataflix/MyAccount">
                    <table><tbody>
                        <caption>Current Rentals</caption>
                        <%
                            if (alR.size()>0)
                            {
                                for (int i=0; i<alR.size(); i++)
                                {
                        %>
                        <tr>
                            <td><a <%="href=\"./movie.jsp?id="+alR.get(i).getId()+"\""%> ><img class="fancyBox" <%="id=\"movie"+alR.get(i).getId()+"\""%> <%="style=\"background-image: url(./images/movies/"+alR.get(i).getGenre().toLowerCase()+"/movie"+alR.get(i).getId()+".jpg\""%>/></a></td>
                            <td><span <%="class=\""+alR.get(i).getGenre().toLowerCase()+"Clr\"" %>><a <%="href=\"./movie.jsp?id="+alR.get(i).getId()+"\""%>><h1><%=alR.get(i).getTitle()%> </h1></a></span></td>
                            <td class="smaller"><input type="checkbox" name="retMovie" <%="value=\""+alR.get(i).getId()+"\""%>/>Return this movie?</td>
                        </tr>
                        <%
                                }
                            }
                            else
                            {
                        %>
                        <tr>
                            <td><h1>You have no movies rented at this time</h1></td>
                        </tr>
                        <%  }%>
                    </tbody></table>
                        <%
                            if (alR.size()>0)
                            {
                        %>
                    <input id="rentSub" name="rentSub" value="Save changes" type="submit"/>
                        <%  }%>
                </form></td>
			
                <td><form id="queue" method="post" action="http://localhost:8080/dataflix/MyAccount">
                    <table>
                        <caption>My Queue</caption>
                        <%
                            if (alQ.size()>0)
                            {
                                for (int i=0; i<alQ.size(); i++)
                                {
                        %>
                        <tr>
                            <td rowspan="2"><a <%="href=\"./movie.jsp?id="+alQ.get(i).getId()+"\""%> ><img class="fancyBox" <%="id=\"movie"+alQ.get(i).getId()+"\""%> <%="style=\"background-image: url(./images/movies/"+alQ.get(i).getGenre().toLowerCase()+"/movie"+alQ.get(i).getId()+".jpg\""%>/></a></td>
                            <td rowspan="2"><span <%="class=\""+alQ.get(i).getGenre().toLowerCase()+"Clr\"" %>><a <%="href=\"./movie.jsp?id="+alQ.get(i).getId()+"\""%>><h1><%=alQ.get(i).getTitle()%> </h1></a></span></td>
                            <td class="smaller"><input type="checkbox" name="qMovie" <%="value=\""+alQ.get(i).getId()+"\""%>/>Remove from queue?</td>
                        </tr>
                        <tr><td class="smaller"><input type="checkbox" name="renMovie" <%="value=\""+alQ.get(i).getId()+"\""%>/>Rent this movie</td></tr>
                        
                        <%      }
                            }
                            else
                            {
                        %>
                        <tr>
                            <td><h1>You have no movies in your queue</h1></td>
                        </tr>
                        <%  }%>
                    </table>
                    <%
                        if (alQ.size()>0)
                        {
                    %>
                    <input id="queueSub" name="queueSub" value="Save changes" type="submit"/>
                    <%  }%>
                </form></td>
			
                <td id="past">
                    <table><tbody>
                    <caption>Rental History</caption>
                        <%
                            if (alP.size()>0)
                            {
                                for (int i=0; i<alP.size(); i++)
                                {
                        %>
                        <tr>
                            <td><a <%="href=\"./movie.jsp?id="+alP.get(i).getId()+"\""%> ><img class="fancyBox" <%="id=\"movie"+alP.get(i).getId()+"\""%> <%="style=\"background-image: url(./images/movies/"+alP.get(i).getGenre().toLowerCase()+"/movie"+alP.get(i).getId()+".jpg\""%>/></a></td>
                            <td><span <%="class=\""+alP.get(i).getGenre().toLowerCase()+"Clr\"" %>><a <%="href=\"./movie.jsp?id="+alP.get(i).getId()+"\""%>><h1><%=alP.get(i).getTitle()%> </h1></a></span></td>
                        </tr>
                        <%      }
                            }
                            else
                            {
                        %>
                        <tr>
                            <td><h1>You haven't rented any movies yet</h1></td>
                        </tr>
                        <%  }%>
                    </tbody></table>
                </td>
            </tbody></table>
        </div>	
        
        
    <div id="footer"/>
    
    
</body>
</html>
